package com.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.Constants;
import com.example.common.Result;
import com.example.entry.User;
import com.example.entry.dto.UserDto;
import com.example.exception.ServiceException;
import com.example.mapper.UserMapper;
import com.example.service.IUserService;
import com.example.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService{

    @Autowired
    private UserMapper userMapper;
/*
* 管理员登录注册
* */
//    注册
    @Override
    public Result userRegister(User user) {
//        检查用户名是否已存在
        String check = userMapper.userCheck(user.getUsername());
        if(check != null){
            return Result.error("300","用户已存在");
        }
        try {
            user.setUsername(user.getUsername());
            user.setPassword(SecureUtil.md5(user.getPassword()));
            user.setNickname(user.getNickname());
            user.setStatus(0);
            userMapper.insert(user);
            return Result.success("注册成功");
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500, "注册失败");
        }
    }

    //    登录
    @Override
    public UserDto login(UserDto userDto){
        UserDto user = loginValidate(userDto);
        return user;
    }

    //    登录验证
    public UserDto loginValidate(UserDto userDto){
        // md5验证
        userDto.setPassword(SecureUtil.md5(userDto.getPassword()));
//        查询数据库的账号密码
        User user = userMapper.selectUser(userDto.getUsername(), userDto.getPassword());
        if(Objects.isNull(user)){
            throw new ServiceException(Constants.CODE_600, "用户不存在！");
        }
        BeanUtil.copyProperties(user, userDto, true);
        // 设置token
        String token = TokenUtils.createToken(user.getId().toString());
        userDto.setToken(token);
        return userDto;
    }

    //    注册
    private Result getUserInfo(User user){
//        检查用户名和密码是否输入
        if(user.getUsername().isEmpty() || user.getPassword().isEmpty()){
            return Result.error(Constants.CODE_500,"请输入用户名和密码");
        }
        String username = user.getUsername();
        String password = user.getPassword();
//        获取数据库的用户名和密码
        User selectUser = userMapper.selectUser(username, password);
        if(selectUser.getStatus() != 0){
            throw new ServiceException(Constants.CODE_500, "该账号不是管理员");
        }
//        当数据查询不到数据时，返回提示错误
        if(selectUser == null){
            return Result.error(Constants.CODE_500,"用户名或密码错误，请重试");
        }
        return Result.success("success",selectUser);
    }

    /*
    * 用户登录注册--------------------------------------------------------------------------
    * */
    // 用户登录
    @Override
    public User userWebLogin(User user) {
        user.setPassword(SecureUtil.md5(user.getPassword()));
        System.out.println(user.getPassword());
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        wrapper.eq("password", user.getPassword());
        try {
            User selectOne = userMapper.selectOne(wrapper);
            if(Objects.isNull(selectOne)){
                throw  new ServiceException(Constants.CODE_500, "账号异常，请联系管理员!");
            }
            if(selectOne.getStatus() != 1) {
                throw new ServiceException(Constants.CODE_500, "账号异常，请联系管理员!");
            }
            if(selectOne.getIsDel() == 1){
                throw  new ServiceException(Constants.CODE_500, "该账号已被删除，请联系管理员!");
            }
            return selectOne;
        } catch (Exception e){
            throw new ServiceException(Constants.CODE_500, "账号异常，请联系管理员!");
        }
    }

    // 用户注册
    @Override
    public int userWebRegister(User user) {
        //        检查用户名是否已存在
        String check = userMapper.userCheck(user.getUsername());
        if(check != null){
            throw new ServiceException(Constants.CODE_500, "该账号以存在");
        }
        System.out.println(SecureUtil.md5(user.getPassword()));
        try {
            // 注册时生成md5加密密钥
            user.setPassword(SecureUtil.md5(user.getPassword()));
            user.setUsername(user.getUsername());
            user.setNickname(user.getUsername());
            user.setPhone(user.getPhone());
            return userMapper.insert(user);
        } catch (Exception e){
            throw new ServiceException(Constants.CODE_500, "注册失败");
        }
    }
}
